<?
include_once("variables.php");
include_once("mailer.class.php");
$session_page = "session.php";

$mail = new mailer($_SESSION['business_id']);

if($_SESSION['customer_id']!='') {
	$session_page = "customersession.php";
} else {
	$session_page = "session.php";
}

include_once($absolute_path.$session_page);
include_once("appointment.php");
$email = $_REQUEST['email'];
$customer_id = $_REQUEST['customer_id'];
$obj = $_REQUEST;
$status = 0;
$starttime = strtotime($obj['apptdate']) + (date("H",$obj['starttime']) * 3600) + (date("i",$obj['starttime']) * 60);
$obj['starttime'] = $starttime;

$endtime = $obj['starttime'] + ($obj['duration']*60);
$obj['endtime'] = $endtime; 

$msg = validateAppointmentAdd($obj);

if($msg != '') {
  $status = 0;
} else if($customer_id=="0") {
	if(chkCustomer($email)) {
		$msg .= "Customer with email address '$email' already exists";
	} else {
		$customer_id = createCustomer($obj);
		$mail->createCustomer($customer_id);
		$obj['customer_id'] = $customer_id;
	}
}

if($msg =='') {
	$msg .= finalizeApp($obj);
	if($obj['appt_id']=='') {
		$appt_id = mysql_insert_id();
		$mail->createAppointment($appt_id);
	} else {
		$mail->createAppointment($obj['appt_id'],1);
	}
	
	$status = 1;
}

?>
<data>
	<status><?=$status?></status>
	<msg><?=$msg?></msg>
</data>